﻿using AutoMapper;
using B.S.Packaged.Domain;
using B.S.Packaged.ErrorCode;
using B.S.Packaged.WebAPI.Read.Application.Command.CompletedStorageCommand;
using MediatR;
using SqlSugar;

namespace B.S.Packaged.WebAPI.Read.Application.CommandHander.CompletedStorageHandler
{
    public class GetCompletedStorageHandler : IRequestHandler<GetCompletedStorageCommand, APIResult<APIPaging<CompletedStorage>>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;
        public GetCompletedStorageHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }
        public async Task<APIResult<APIPaging<CompletedStorage>>> Handle(GetCompletedStorageCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPaging<CompletedStorage>> reat = new APIResult<APIPaging<CompletedStorage>>();
            var listtt = await db.Queryable<CompletedStorage>().Where(x => !x.IsDel).ToListAsync();
            if (!string.IsNullOrEmpty(request.factory))
            {
                listtt = listtt.Where(x => x.factory.Contains(request.factory)).ToList();
            }
            if (!string.IsNullOrEmpty(request.workOrderNo))
            {
                listtt = listtt.Where(x => x.workOrderNo.Contains(request.workOrderNo)).ToList();
            }
            if (!string.IsNullOrEmpty(request.finishedProductBatch))
            {
                listtt = listtt.Where(x => x.finishedProductBatch.Contains(request.finishedProductBatch)).ToList();
            }
            if (!string.IsNullOrEmpty(request.productionLine))
            {
                listtt = listtt.Where(x => x.productionLine.Contains(request.productionLine)).ToList();
            }
            if (!string.IsNullOrEmpty(request.team))
            {
                listtt = listtt.Where(x => x.team.Contains(request.team)).ToList();
            }
            if (!string.IsNullOrEmpty(request.shift))
            {
                listtt = listtt.Where(x => x.shift.Contains(request.shift)).ToList();
            }
            if (!string.IsNullOrEmpty(request.materialCode))
            {
                listtt = listtt.Where(x => x.materialCode.Contains(request.materialCode)).ToList();
            }
            if (!string.IsNullOrEmpty(request.accessType))
            {
                listtt = listtt.Where(x => x.accessType.Contains(request.accessType)).ToList();
            }
            if (!string.IsNullOrEmpty(request.TicketNumber))
            {
                listtt = listtt.Where(x => x.TicketNumber.Contains(request.TicketNumber)).ToList();
            }
            if (!string.IsNullOrEmpty(request.workoOderType))
            {
                listtt = listtt.Where(x => x.workoOderType.Contains(request.workoOderType)).ToList();
            }
            if (request.transactionTime != null)
            {
                listtt = listtt.Where(x => x.transactionTime.Date == request.transactionTime.Value.Date).ToList();
            }
            if (request.transactionEndTime != null)
            {
                listtt = listtt.Where(x => x.transactionEndTime.Date == request.transactionEndTime.Value.Date).ToList();
            }

            int totalCount = listtt.Count();
            int PageCount = (int)Math.Ceiling(totalCount * 1.0 / request.pageSize);
            listtt = listtt.OrderByDescending(a => a.id).Skip((request.pageindex - 1) * request.pageSize).Take(request.pageSize).ToList();
            APIPaging<CompletedStorage> paging = new APIPaging<CompletedStorage>()
            {
                TotalCount = totalCount,
                TotalPage = PageCount,
                list = listtt.ToList(),

            };

            reat.code = APIResultCode.success;
            reat.msg = "完工入库表显示成功";
            reat.data = paging;
            return reat;
        }
    }
}
